/*
 * Copyright (C) 2003, 2004 Rex Ford
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * DEVELOPERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */ 
 
package GamePlay;
import GamePlay.GameObjects.*;

public class GOFactory {
	private GameEnv theEnv;
	private int theLevel;
	private int shipsLeft;
	
	private long timePassed=0;
	private long sinceLastTile=0;
	private long sinceLastShip=0;
	private long sinceLastInteractor=0;
	
	public GOFactory(int level, GameEnv env) {
		theEnv = env;
		theLevel = level;
		shipsLeft = level * 25;
	}
	
	public void pulse(long time) {
		timePassed+=time;
		sinceLastTile+=time;
		sinceLastShip+=time;
		sinceLastInteractor+=time;
		
		if(shipsLeft>0)
			addShips();
		addTiles();
		addInteractors();
	}
	
	private void addShips() {
			if (sinceLastShip > 4000) 
			{
				sinceLastShip=0;
				shipsLeft--;
				double which=Math.random();
				if(which>0.5)
					theEnv.addGO(new Saucer(theEnv), 2);
				else
					theEnv.addGO(new Saucer2(theEnv),2);
			}
	}
	
	private void addTiles() {
		if (sinceLastTile > 290) {
			sinceLastTile=0;
			theEnv.addGO(new Star(theEnv), 1);
		}
	}
	
	private void addInteractors() {
		if(sinceLastInteractor >= 20000){
			sinceLastInteractor=0;
			addRandomInteractor();
		}
	}
	
	private void addRandomInteractor(){
		double which=Math.random()*100;
		if(which<70)
			theEnv.addGO(new Health(theEnv),2);
		else
			theEnv.addGO(new GunPlus(theEnv),2);
		
	}
	
}